angular.module("application")
.filter('eventTime', function(renderProcess, BASE_URL) {
	return function(array, century, year, range, nation) {
		var arrx = [];
		angular.forEach(array, function(item) {

			arrNation = [];
			if(item.countries && item.countries.length > 0) {
				angular.forEach(item.countries, function(ctr) {
					arrNation.push(ctr.id);
				})
			}

			frCent = item.from.century;
			frYear = item.from.year;

			toCent = null; toYear = null;
			if(item.to) {
				toCent = item.to.century;
				toYear = item.to.year;
			}
			strFrCent = renderProcess.makeCenturyCaption(frCent);
			strToCent = renderProcess.makeCenturyCaption(toCent);

			if(toYear < 0) toYear = Math.abs(toYear) + " TCN";
			if(frYear < 0) frYear = Math.abs(frYear) + " TCN";

			item.time = "";
			if(frYear && !toCent) {
				item.time = "Từ năm " + frYear;
			}
			else if(frYear && !toYear) {
				item.time = "Từ năm " + frYear + " đến " + strToCent;
			}
			else if(frYear && toYear) {
				item.time = "Từ năm " + frYear + " đến năm " + toYear;
			}			
			else if(!frYear && toYear) {
				item.time = "Từ " + strFrCent + " đến năm " + toYear;
			}
			else if(toCent) {
				str = "Từ " + strFrCent + " đến " + strToCent;
				str = str.replace("đến Cách đây khoảng", "đến");
				str = str.replace("đến Cách đây", "đến");
				item.time = str;
			}
			else item.time = strFrCent;

			// item.region = BASE_URL + "assets/images/wo-stamp.png";
			// if(item.type == 'VN') item.region = BASE_URL + "assets/images/vn-stamp.png";
			if(item.type == 'WO') item.type = "TG";

			if(!range.frYear && !range.toYear) {
				if(toCent && century <= toCent && century >= frCent) { 
						arrx.push(item); 
				}
				else if(century == frCent) { 
						arrx.push(item); 
				}

				if(year) {
					for(i in arrx) {
						if( (arrx[i].from.year && arrx[i].from.year > year) ||
							(!arrx[i].to && arrx[i].from.year < year) ||
							(arrx[i].to && arrx[i].to.year && arrx[i].to.year < year))
						{
							arrx.splice(i, 1);
						}
					}
				}
			}
			else {

				paramFrYear = range.frYear;
				paramFrCentury = Math.floor(paramFrYear / 100);
				if(paramFrCentury >= 0) paramFrCentury++;

				paramToYear = range.toYear;
				paramToCentury = Math.floor(paramToYear / 100);
				if(paramToCentury >= 0) paramToCentury++;

				if(toCent && paramFrCentury <= toCent && paramToCentury >= frCent) { 
					if(nation == null)
						arrx.push(item); 
					else if(arrNation.indexOf(nation._id) != -1)
						arrx.push(item);
				}
				else if(paramFrCentury <= frCent && frCent <= paramToCentury) { 
					if(nation == null)
						arrx.push(item); 
					else if(arrNation.indexOf(nation._id) != -1)
						arrx.push(item);
				}

				for(i in arrx) {
					if( (arrx[i].to && arrx[i].to.year && arrx[i].to.year < paramFrYear) ||
						(arrx[i].from.year && arrx[i].from.year > paramToYear) ||
						(arrx[i].to && !arrx[i].to.year && arrx[i].from.year && arrx[i].from.year < paramFrYear))
					{
						arrx.splice(i, 1);
					}
				}
			}
		});
		return arrx;
	}
})
.filter('toCentury', function() {
	return function(array, fromCentury) {
		arrx = [];
		angular.forEach(array, function(cent) {
			if(cent.val >= fromCentury) {
				arrx.push(cent);
			}
		});
		return arrx;
	}
})
.filter('chapterFilter', function() {
	return function(input, index) {
		i = index;
		roman = [
			'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'
		]
		// if(typeof input == "undefined") return "\u00a0";
		if(typeof input == "undefined") return "";
		return "Chương " + roman[i] + ": " + input;
	}
})

.filter('categoryFilter', function() {
	return function(arr, id) {
		if(typeof id == 'undefined' || id == "") return arr;

		arrx = [];

		if(id == "vn" || id == "wo") {
			angular.forEach(arr, function(item) {
				cat = item.category;
				if(cat.search(id) != -1) {
					arrx.push(item);
				}
			});
			return arrx;
		};


		prefix = id.slice(0,-2);
		type = id.substr(id.length - 2, id.length);
		angular.forEach(arr, function(item) {
			cat = item.category;
			if(cat.search(prefix) != -1 && cat.search(type) != -1) {
				arrx.push(item);
			}
		})
		return arrx;
	}
})

.filter('vnSort', function() {
	convert=function(str, index) {
		str = str.toLowerCase();
		str=str.replace("á","a1"); str=str.replace("à","a2"); str=str.replace("ả","a3"); str=str.replace("ã","a4"); str=str.replace("ạ","a5");
		str=str.replace("ă","az");str=str.replace("ắ","az1"); str=str.replace("ằ","az2"); str=str.replace("ẳ","az3"); str=str.replace("ẵ","az4"); str=str.replace("ặ","az5");
		str=str.replace("â","azz");str=str.replace("ấ","azz1"); str=str.replace("ầ","azz2"); str=str.replace("ẩ","azz3"); str=str.replace("ẫ","azz4"); str=str.replace("ậ","azz5");
		str=str.replace("ó","o1"); str=str.replace("ò","o2"); str=str.replace("ỏ","o3"); str=str.replace("õ","o4"); str=str.replace("ọ","o5");
		str=str.replace("ô","oz");str=str.replace("ố","oz1"); str=str.replace("ồ","oz2"); str=str.replace("ổ","oz3"); str=str.replace("ỗ","oz4"); str=str.replace("ộ","oz5");
		str=str.replace("ơ","ozz");str=str.replace("ớ","ozz1"); str=str.replace("ờ","ozz2"); str=str.replace("ở","ozz3"); str=str.replace("ỡ","ozz4"); str=str.replace("ợ","ozz5");
		str=str.replace("ú","u1"); str=str.replace("ù","u2"); str=str.replace("ủ","u3"); str=str.replace("ũ","u4"); str=str.replace("ụ","u5");
		str=str.replace("ư","uz");str=str.replace("ứ","uz1"); str=str.replace("ừ","uz2"); str=str.replace("ử","uz3"); str=str.replace("ữ","uz4"); str=str.replace("ự","uz5");
		str=str.replace("é","e1"); str=str.replace("è","e2"); str=str.replace("ẻ","e3"); str=str.replace("ẽ","e4"); str=str.replace("ẹ","e5");
		str=str.replace("ê","ez"); str=str.replace("ế","ez1"); str=str.replace("ề","ez2"); str=str.replace("ể","ez3"); str=str.replace("ễ","ez4"); str=str.replace("ệ","ez5");
		str=str.replace("í","i1"); str=str.replace("ì","i2"); str=str.replace("ỉ","i3"); str=str.replace("ĩ","i4"); str=str.replace("ị","i5");
		str=str.replace("ý","y1"); str=str.replace("ỳ","y2"); str=str.replace("ỷ","y3"); str=str.replace("ỹ","y4"); str=str.replace("ỵ","y5");
		str=str.replace("đ","dz");
		return { 'str': str, 'i': index }
		// return str;
	}
	return function(array, type) {
		arr = [];
		arrx = [];
		arrSort = [];

		angular.forEach(array, function(item) {
			arr.push(item);
		});

		angular.forEach(array, function(item, i) {
			cvt = convert(item[type], i);
			arrSort.push(cvt);
		});
		arrSort.sort(function(a, b) {
			if(a.str > b.str) return 1;
			if(a.str < b.str) return -1;
			return 0;
		});

		angular.forEach(arrSort, function(item) {
			arrx.push(arr[item.i]);
		})
		return arrx;
	}
})

.filter('simpleFilter', function() {

	removeSign = function(str) {
		str = str.toLowerCase();
		str = str.replace(/ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ/g,"o");
		str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");
		str = str.replace(/é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ/g,"e");
		str = str.replace(/ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự/g,"u");
		str = str.replace(/í|ì|ỉ|ĩ|ị/g,"i");
		str = str.replace(/ý|ỳ|ỷ|ỹ|ỵ/g,"y");
		str = str.replace(/đ/g,"d");
		return str;
	}

	return function(array, str, type) {
		arrx = [];
		str = removeSign(str);

		angular.forEach(array, function(item) {
			desctrip = removeSign(item[type]);
			if(desctrip.search(str) != -1)
				arrx.push(item);
		});
			
		return arrx;
	}
})

.filter('catArrFilter', function() {
	removeTail = function(str) {
		return str.substring(0, str.length - 2);
	}
	return function(array, str, type) {
		arrx = [];
		angular.forEach(array, function(item) {
			if(str == '') {
				arrx.push(item);
			}
			else if(item.cat.length > 0) {
				for(i in item.cat) {
					sou = removeTail(item.cat[i]);
					des = removeTail(str);
					if(sou.search(des) != -1) arrx.push(item);
					break;
				}
			}
		});
		return arrx;
	}
})

.filter('regionFilter', function() {
	return function(array, str, type) {
		arrx = [];
		angular.forEach(array, function(item) {			
			if(item.cat.length > 0) {
				for(i in item.cat) {
					sou = item.cat[i].slice(-2);
					if(sou == str) arrx.push(item);
					break;
				}
			}
		});
		return arrx;
	}
})

.filter('catFilter', function() {
	return function(array, region) {
		arrx = [];
		if(region == "vn") {
			angular.forEach(array, function(cat) {
				id = cat._id;
				if (id.indexOf('vn') != -1) arrx.push(cat);
			});
			return arrx;
		}
		else {
			angular.forEach(array, function(cat) {
				id = cat._id;
				if (id.indexOf('tg') != -1) arrx.push(cat);
			});
			return arrx;
		}
	}
})

.filter('removeLesson', function() {
	return function(input) {
		str = input;
		if(str) {
			index = str.search(/bài /i);
			if(index == 0 && str[4] >= '0' && str[4] <= '9') {
				arr = str.split(': ');
				arr.splice(0,1);
				str = arr.join();
				return str; 
			}
			return str;
		}
	}
})

.filter('removeEmptyQuestion', function() {
	return function(array) {
		arr = [];
		for(i in array) {
			if(array[i].questions.length > 0) {
				arr.push(array[i]);
			}
		}
		return arr;
	}
})